package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import model.ContactModel;
import model.ContactsModel;

public class View extends JFrame implements Observer {
	
	//Parameters
	private static final long serialVersionUID = 1L;
	//private static final String FORM_CONTACT_TITLE = "CONTACT FORM";
	private static final String SEARCH_CONTACT_TITLE = "SEARCH CONTACT";
	private static final String ADD_BUTTON = "Save";
	private static final String MODIFY_BUTTON = "Modify";
	private static final String DELETE_BUTTON = "Delete";
	private static final String CLEAR_BUTTON = "Clear";
	private static final String DISPLAY_BY_ALPHABETICAL_BUTTON = "Display By Alphabetical";
	private static final String DISPLAY_BY_GROUP_BUTTON = "Display By Group";
	private static final String DISPLAY_BY_CREATINGTIME_BUTTON = "Display By Creating Time";
	private static final String FIRST_NAME = "First Name :";
	private static final String LAST_NAME = "Last Name :";
	private static final String HOME_ADDRESS = "Home Address :";
	private static final String COMPANY_ADDRESS = "Company Address :";
	private static final String PERSONAL_MOBILE_PHONE = "Personal Mobile Phone :";
	private static final String PERSONAL_HOME_PHONE = "Personal Home phone :";
	private static final String PROFESSIONAL_MOBILE_PHONE = "Professional Mobile Phone :";
	private static final String PROFESSIONAL_OFFICE_PHONE = "Professional Office phone :";
	private static final String HOME_PAGE = "Home Page :";
	private static final String PROFESSIONAL_EMAIL = "Professional Email :";
	private static final String PERSONAL_EMAIL = "Personal Email :";
	private static final String OTHER = "Other :";
	private static final String PHOTO = "Photo :";

	private static final String GROUP = "GROUP :";
	private static final String CREATE = "Create";
	private String[] groupStrings = { "friends", "family", "companies","co-workers"};

	//GUI Instance
	public ContactsModel model;
	public JPanel panelBasic, panelAddContact1,panelAddContact2,panelAddContact3, panelSearchContact;
	public JButton buttonAddContact, buttonClearContact, buttonModifyContact,
			buttonDeleteContact,
			buttonDisplayByAlphabetical, buttonDisplayByGroup,
			buttonDisplayByCreatingTime, buttonCreateContact,
			buttonNextPage, buttonPreviousPage;
	public JLabel labelFirstName, labelLastName, labelHomeAddress,
			labelCompanyAddress, labelPersonalPhoneMobile,
			labelPersonalPhoneHome, labelProfessionalPhoneMobile,
			labelProfessionalPhoneOffice, labelHomePage, lablEmailProfessional,
			lablEmailPersonal, labelPhoto,labelGroup,labelOther;
	public JTextField textFieldFirstName,
			textFieldLastName, textFieldHomeAddress, textFieldCompanyAddress,
			textFieldPersonalPhoneMobile, textFieldPersonalPhoneHome,
			textFieldProfessionalPhoneMobile, textFieldProfessionalPhoneOffice,
			textFieldHomePage, textFieldEmailProfessional,
			textFieldEmailPersonal,textFieldImage, textFieldOther;
	public JComboBox comboBoxGroup;	
	public JFileChooser fileChooserImage;


	public JScrollPane scrollPaneDisplayContactPersonName = new JScrollPane();
	public DefaultListModel defaultListModel = new DefaultListModel();
	public JList listDisplayerContactPersonName = new JList(defaultListModel);

	/**
	 * Constructor
	 * @param model
	 */
	public View(ContactsModel model) {
		this.model = model;

		// create 3 panels in this frame
		panelBasic = new JPanel();
		panelAddContact1 = new JPanel();
		panelAddContact2 = new JPanel();

		panelSearchContact = new JPanel();

		panelBasic.add(panelAddContact1);
		panelBasic.add(panelAddContact2);

		panelBasic.add(panelSearchContact);
		panelBasic = (JPanel) this.getContentPane();
		// set off border title
		/*
		 * panelAddContact.setBorder(BorderFactory.createTitledBorder(FORM_CONTACT_TITLE));
		 */
				
		panelSearchContact.setBorder(BorderFactory
				.createTitledBorder(SEARCH_CONTACT_TITLE));

		// set off the list of person name view
		scrollPaneDisplayContactPersonName.getViewport().setView(
				listDisplayerContactPersonName);
		scrollPaneDisplayContactPersonName
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		// set off buttons
		buttonCreateContact = new JButton(CREATE);
		buttonCreateContact.setEnabled(false);
		buttonAddContact = new JButton(ADD_BUTTON);
		buttonAddContact.setEnabled(true);
		buttonModifyContact = new JButton(MODIFY_BUTTON);
		buttonModifyContact.setEnabled(false);
		buttonDeleteContact = new JButton(DELETE_BUTTON);
		buttonDeleteContact.setEnabled(false);
		buttonClearContact = new JButton(CLEAR_BUTTON);
		buttonDisplayByAlphabetical = new JButton(
				DISPLAY_BY_ALPHABETICAL_BUTTON);
		buttonDisplayByGroup = new JButton(DISPLAY_BY_GROUP_BUTTON);
		buttonDisplayByCreatingTime = new JButton(
				DISPLAY_BY_CREATINGTIME_BUTTON);
		// set off labels
		labelFirstName = new JLabel(FIRST_NAME);
		labelFirstName.setFont(new Font("Serif", Font.BOLD, 28));
		labelLastName = new JLabel(LAST_NAME);
		labelLastName.setFont(new Font("Serif", Font.BOLD, 28));
		labelHomeAddress = new JLabel(HOME_ADDRESS);
		labelHomeAddress.setFont(new Font("Serif", Font.BOLD, 28));
		labelCompanyAddress = new JLabel(COMPANY_ADDRESS);
		labelCompanyAddress.setFont(new Font("Serif", Font.BOLD, 28));
		labelPersonalPhoneMobile = new JLabel(PERSONAL_MOBILE_PHONE);
		labelPersonalPhoneMobile.setFont(new Font("Serif", Font.BOLD, 28));

		labelPersonalPhoneHome = new JLabel(PERSONAL_HOME_PHONE);
		labelPersonalPhoneHome.setFont(new Font("Serif", Font.BOLD, 28));

		labelProfessionalPhoneMobile = new JLabel(PROFESSIONAL_MOBILE_PHONE);
		labelProfessionalPhoneMobile.setFont(new Font("Serif", Font.BOLD, 28));

		labelProfessionalPhoneOffice = new JLabel(PROFESSIONAL_OFFICE_PHONE);
		labelProfessionalPhoneOffice.setFont(new Font("Serif", Font.BOLD, 28));
		
		labelHomePage = new JLabel(HOME_PAGE);
		labelHomePage.setFont(new Font("Serif", Font.BOLD, 28));

		lablEmailProfessional = new JLabel(PROFESSIONAL_EMAIL);
		lablEmailProfessional.setFont(new Font("Serif", Font.BOLD, 28));

		lablEmailPersonal = new JLabel(PERSONAL_EMAIL);
		lablEmailPersonal.setFont(new Font("Serif", Font.BOLD,28));

		labelPhoto = new JLabel(PHOTO);
		labelPhoto.setSize(new Dimension(200,200));
		labelPhoto.setFont(new Font("Serif", Font.BOLD, 28));

		labelGroup = new JLabel(GROUP);
		labelGroup.setFont(new Font("Serif", Font.BOLD, 28));

		labelOther = new JLabel(OTHER);
		labelOther.setFont(new Font("Serif", Font.BOLD, 28));

		// set off text field
		textFieldFirstName = new JTextField();
		textFieldLastName = new JTextField(10);
		textFieldHomeAddress = new JTextField(10);
		textFieldCompanyAddress = new JTextField(10);
		textFieldPersonalPhoneMobile = new JTextField(10);
		textFieldPersonalPhoneHome = new JTextField(10);
		textFieldProfessionalPhoneMobile = new JTextField(10);
		textFieldProfessionalPhoneOffice = new JTextField(10);
		textFieldHomePage = new JTextField(10);
		textFieldEmailProfessional = new JTextField(10);
		textFieldEmailPersonal = new JTextField(10);
		textFieldOther = new JTextField(10);
		comboBoxGroup = new JComboBox(this.groupStrings);
		comboBoxGroup.setSelectedIndex(0);
		fileChooserImage = new JFileChooser();
		fileChooserImage.setMultiSelectionEnabled(false);
		
		textFieldImage = new JTextField(50);
		textFieldImage.setEditable(false);
		
		// GridBagLayout
		GridBagLayout layout = new GridBagLayout();
		panelAddContact1.setLayout(layout);
		panelAddContact2.setLayout(layout);

		// adds into THE panelAddContact
		// panelAddContact.add(labelPhoto);
		panelAddContact1.add(labelFirstName);
		panelAddContact2.add(textFieldFirstName);
		panelAddContact1.add(labelLastName);
		panelAddContact2.add(textFieldLastName);
		panelAddContact1.add(labelHomeAddress);
		panelAddContact2.add(textFieldHomeAddress);
		panelAddContact1.add(labelCompanyAddress);
		panelAddContact2.add(textFieldCompanyAddress);
		panelAddContact1.add(labelPersonalPhoneMobile);
		panelAddContact2.add(textFieldPersonalPhoneMobile);
		panelAddContact1.add(labelPersonalPhoneHome);
		panelAddContact2.add(textFieldPersonalPhoneHome);
		panelAddContact1.add(labelProfessionalPhoneMobile);
		panelAddContact2.add(textFieldProfessionalPhoneMobile);
		panelAddContact1.add(labelProfessionalPhoneOffice);
		panelAddContact2.add(textFieldProfessionalPhoneOffice);
		panelAddContact1.add(labelHomePage);
		panelAddContact2.add(textFieldHomePage);
		panelAddContact1.add(lablEmailProfessional);
		panelAddContact2.add(textFieldEmailProfessional);
		panelAddContact1.add(lablEmailPersonal);
		panelAddContact2.add(textFieldEmailPersonal);
		panelAddContact1.add(labelGroup);
		panelAddContact2.add(comboBoxGroup);
		panelAddContact1.add(labelPhoto);
		panelAddContact2.add(textFieldImage);
		panelAddContact1.add(labelOther);
		panelAddContact2.add(textFieldOther);
		// panelAddContact.setLayout(new FlowLayout(FlowLayout.LEADING, 10,
		// 40));
		


		
		panelAddContact2.add(buttonCreateContact);
		panelAddContact2.add(buttonModifyContact);
		panelAddContact2.add(buttonDeleteContact);
		panelAddContact2.add(buttonAddContact);

		
		 panelAddContact1.setLayout(new BoxLayout(panelAddContact1,BoxLayout.Y_AXIS));
		 panelAddContact2.setLayout(new BoxLayout(panelAddContact2,BoxLayout.Y_AXIS));

	
	




		// set off the search function in the top of the window
		//this.add(panelAddContact1);
		add(panelAddContact1, BorderLayout.WEST);//
		add(panelAddContact2, BorderLayout.EAST);
		//add(panelAddContact3);

		//this.add(panelAddContact2);

		// adds into THE panelSearchContact
		panelSearchContact.add(buttonDisplayByAlphabetical);
		panelSearchContact.add(buttonDisplayByGroup);
		panelSearchContact.add(buttonDisplayByCreatingTime);
		panelSearchContact.add(scrollPaneDisplayContactPersonName);
		// set off the add function in the bottom of the window
		add(panelSearchContact, BorderLayout.SOUTH);//

		this.model.addObserver(this);

		// set off the parameters of the window
		this.setSize(1021, 700);
		this.setTitle("Contact Book");
		this.setVisible(true);

	}

	/**
	 * Initialize all textfields and combobox
	 */
	public void clearform() {
		for (Component C : this.panelAddContact2.getComponents()){

		    if (C instanceof JTextField){

		        ((JTextField) C).setText(null); //abstract superclass
		    }else if(C instanceof JComboBox){
		    	((JComboBox) C).setSelectedIndex(0);
		    }
		}
		
		
	}
	
	/**
	 * Enable or disenable all textfields and combobox
	 * @param enable
	 */
	public void enableform(boolean enable){
		for (Component C : this.panelAddContact2.getComponents()){

		    if (C instanceof JTextField){

		        ((JTextField) C).setEditable(enable); //abstract superclass
		    }else if(C instanceof JComboBox){
		    	((JComboBox) C).setEnabled(enable);
		    }
		}
	}


	/**
	 * Implementation of Observer
	 */
	@Override
	public void update(Observable arg0, Object arg1) {

		ArrayList<ContactModel> contacts = this.model.getContacts();
		defaultListModel.removeAllElements();
		
		for (ContactModel contact : contacts) {
			defaultListModel.addElement(contact.getPersonName());
		}
		System.out.println("Update:"+defaultListModel.getSize());

	}


}
